//
//  NSDictionary+DataFormat.m
//  OMS
//
//  Created by goldeney on 14/10/21.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "NSDictionary+DataFormat.h"

@implementation NSDictionary (DataFormat)

- (id)objectFormatForKey:(id)aKey
{
    if (!aKey) return nil;
    id objectValue = [self objectForKey:aKey];
    
    //赋值给nil之外的其他空值
    if (objectValue == [NSNull null])
    return nil;
    
    if ([objectValue isKindOfClass:[NSString class]] && [objectValue isEqualToString:@""])
    return nil;
    
    return objectValue;
}

- (NSString*)objectFormatStringForKey:(id)aKey
{
    id value = [self objectFormatForKey:aKey];
    if (value) return [NSString stringWithFormat:@"%@",value];
    else return @"";
}

- (NSNumber*)objectFormatFloatNumberForKey:(id)aKey
{
    id value = [self objectFormatForKey:aKey];
    if (value && [value isKindOfClass:[NSNumber class]])
        return value;
    
    if (value && [value isKindOfClass:[NSString class]])
        return [NSNumber numberWithFloat:[value floatValue]];
    
    else return [NSNumber numberWithFloat:0];
}

- (CGFloat)objectFormatFloatForKey:(id)aKey
{
    NSNumber *value = [self objectFormatFloatNumberForKey:aKey];
    return [value floatValue];
}

- (NSString*)objectFormatFloatStringForKey:(id)aKey retain:(int)digit
{
    CGFloat floatValue = [self objectFormatFloatForKey:aKey];
    NSString *format = [NSString stringWithFormat:@"%%0.%df",digit];
    return [NSString stringWithFormat:format,floatValue];
}

- (NSInteger)objectFormatIntegerForKey:(id)aKey
{
    id value = [self objectFormatForKey:aKey];
    if (value && [value isKindOfClass:[NSNumber class]])
    return [value integerValue];
    
    if (value && [value isKindOfClass:[NSString class]])
        return [value integerValue];
    
    else return 0;
}

- (NSNumber*)objectFormatIntegerNumberForKey:(id)aKey
{
    NSInteger value = [self objectFormatIntegerForKey:aKey];
    return [NSNumber numberWithInteger:value];
}

- (NSString*)objectFormatIntStringForKey:(id)aKey
{
    NSInteger value = [self objectFormatIntegerForKey:aKey];
    return [NSString stringWithFormat:@"%ld",value];
}

#pragma mark 其他延伸方法

- (NSString*)objectFormatStringForKey:(id)aKey format:(NSString*)format defaults:(NSString*)defaultValue
{
    id value = [self objectFormatForKey:aKey];
    if (value) {
        NSString *valueString = [NSString stringWithFormat:@"%@",value];
        if (valueString.length > 0) return [NSString stringWithFormat:format,value];
        else return defaultValue;
    } else {
        return defaultValue;
    }
}

- (NSString*)objectFormatStringForKey:(id)aKey defaults:(NSString*)defaultValue
{
    id value = [self objectFormatForKey:aKey];
    if (value) {
        NSString *valueString = [NSString stringWithFormat:@"%@",value];
        return (valueString.length > 0) ? valueString : defaultValue;
    } else {
        return defaultValue;
    }
}

- (NSString*)objectFormatFloatStringForKey:(id)aKey retain:(int)digit format:(NSString*)format
{
    NSString *value = [self objectFormatFloatStringForKey:aKey retain:digit];
    if (format && format.length >0) return [NSString stringWithFormat:format,value];
    else return value;
}

- (NSString*)objectFormatFloatStringForKey:(id)aKey retain:(int)digit except:(int)except exceptFormat:(NSString*)exceptStr format:(NSString*)format
{
    CGFloat floatValue = [self objectFormatFloatForKey:aKey];
    CGFloat floatValueExcept = 0;
    if(except != 0) floatValueExcept = floatValue / except;
    
    if (fabs(floatValueExcept) > 1.0 && exceptStr && exceptStr.length > 0) {
        NSString *formatString = [NSString stringWithFormat:@"%%0.%df",digit];
        NSString *formatValue = [NSString stringWithFormat:formatString,floatValueExcept];
        return [NSString stringWithFormat:exceptStr,formatValue];
        
    } else {
        NSString *formatString = [NSString stringWithFormat:@"%%0.%df",digit];
        NSString *formatValue = [NSString stringWithFormat:formatString,floatValue];
        if(format && format.length > 0) return [NSString stringWithFormat:format,formatValue];
        else return formatValue;
    }
}

- (CGFloat)objectFormatFloatForKey:(id)aKey retain:(int)digit except:(int)except
{
    CGFloat floatValue = [self objectFormatFloatForKey:aKey];
    CGFloat floatValueExcept = 0;
    if(except != 0) floatValueExcept = floatValue / except;
    else floatValueExcept = floatValue;
    
    NSString *formatString = [NSString stringWithFormat:@"%%0.%df",digit];
    NSString *formatValue = [NSString stringWithFormat:formatString,floatValueExcept];
    return [formatValue floatValue];
}

- (NSString*)objectFormatFloatForKey:(id)aKey retain:(int)digit except:(int)except format:(NSString*)format
{
    CGFloat value = [self objectFormatFloatForKey:aKey retain:digit except:except];
    NSString *formatString = [NSString stringWithFormat:@"%%0.%df",digit];
    NSString *valueString = [NSString stringWithFormat:formatString,value];
    return [NSString stringWithFormat:format,valueString];
}

- (NSString*)objectFormatTimeStringForKey:(id)aKey inFormat:(NSString*)inFormat outFormat:(NSString*)outFormat
{
    NSString *value = [self objectFormatStringForKey:aKey];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:inFormat];
    NSDate *date = [dateFormatter dateFromString:value];
    
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:outFormat];
    NSString *result = [dateFormatter1 stringFromDate:date];
    
    return result;
}

@end
